package com.example.databindingtest.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

abstract class BaseViewFragment<T : ViewDataBinding> : Fragment() {

    protected lateinit var binding: T
    protected lateinit var rootView: View

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        /**
         * 方式一
         */
        /*val view = inflater.inflate(getSubLayoutId(), container, false)
        binding = DataBindingUtil.bind<T>(view)
        return view*/

        /**
         * 方式二
         */
        binding = DataBindingUtil.inflate<T>(inflater, getSubLayoutId(), container, false)
        rootView = binding.root
        return rootView
    }

    abstract fun getSubLayoutId(): Int
}